// pages/operation/ruleSettings/holidaySetting/index.js
import { Holiday } from "../../../../api/api-work/holiday.js";
const holiday = new Holiday();
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
  },
  created() {
    this.getList();
  },

  methods: {
    addHoliday() {
      wx.navigateTo({
        url: `/pages/operation/ruleSettings/addHoliday/index`,
      });
    },
    toDetail(e) {
      const item = e.currentTarget.dataset.query;
      wx.navigateTo({
        url: `/pages/operation/ruleSettings/addHoliday/index?form=${JSON.stringify(
          item
        )}`,
      });
    },
    getList() {
      holiday.getHolidayListAPI({ pageSize: 9999, pageNum: 1 }).then((res) => {
        this.setData({
          list: res.rows,
        });
      });
    },
    deleteHoliday(e) {
      const query = e.currentTarget.dataset.query;
      holiday.deleteHolidayAPI({ id: query.id }).then((res) => {
        this.getList();
      });
    },
    onChangeState(e) {
      const that = this;
      const query = e.currentTarget.dataset.query;
      let title = "";
      if (query.state == 1) {
        title = "确认启用？";
      } else {
        title = "确认停用？";
      }
      wx.showModal({
        //title: "提示",
        content: title,
        success(res) {
          if (res.confirm) {
            holiday
              .updateHolidayAPI({
                id: query.id,
                state: query.state == 0 ? 1 : 0,
                isDelete: 0,
                name: query.name,
                startDate: query.startDate,
                endDate: query.endDate,
                remark: query.remark,
              })
              .then(() => {
                that.getList();
              });
          } else if (res.cancel) {
            //11
          }
        },
        fail(error) {
          // 处理错误情况
          console.error("Modal display error:", error);
        },
      });
    },
    updateHoliday(e) {
      const query = e.currentTarget.dataset.query;
      holiday
        .updateHolidayAPI({
          id: query.id,
          state: query.state === 0 ? 1 : 0,
          isDelete: 0,
          name: query.name,
          startDate: query.startDate,
          endDate: query.endDate,
          remark: query.remark,
        })
        .then((res) => {
          this.getList();
        });
    },
    kongf() {},
  },
});
